/******************************************************************************* 
 * Copyright (C) 2012-2015 Microfountain Technology, Inc. All Rights Reserved. 
 * 
 * Unauthorized copying of this file, via any medium is strictly prohibited.   
 * Proprietary and confidential
 * 
 * Last Modified: 2015-9-18 16:59:32
 ******************************************************************************/
package cn.com.xy.sms.sdk.net;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;

import cn.com.xy.sms.sdk.Iservice.OnlineUpdateCycleConfigInterface;
import cn.com.xy.sms.sdk.constant.Constant;
import cn.com.xy.sms.sdk.dex.DexUtil;
import cn.com.xy.sms.sdk.log.LogManager;
import cn.com.xy.sms.sdk.util.StringUtils;
import android.annotation.SuppressLint;

public class HttpConnection {
    private SSLSocketFactory sslSocketFactory = null;

    public void open(URL url) throws IOException {

        // open a URLConnection if no proxy
        HttpURLConnection conn = (HttpURLConnection) (url.openConnection());

        if (conn instanceof HttpsURLConnection) {
            this.initHttpsConnection((HttpsURLConnection) conn, 1);
        }
    }

    public static HttpsURLConnection createHttpsURLConnectionAndConnect(
            String url) {
        return createHttpsURLConnectionAndConnect(url, 1);
    }

    public static HttpsURLConnection createHttpsURLConnectionAndConnect(
            String url, int type) {
        HttpsURLConnection https = null;
        try {
            HttpConnection httpConnection = new HttpConnection();
            https = (HttpsURLConnection) new URL(url).openConnection();
            httpConnection.initHttpsConnection(https, type);

            /*SDK-663 zhaojiangwei 20161107 start*/
            String referer = DexUtil.getOnLineConfigureData(OnlineUpdateCycleConfigInterface.CONFIG_URL_REFERER);
            if (StringUtils.isNull(referer)) {
                referer = "bizport.cn/66dc91e8b78b1c284027a3eb1be0a70e";
            }
            https.addRequestProperty("referer",  referer);
            /*SDK-663 zhaojiangwei 20161107 end*/
            
            https.connect();
        } catch (Throwable e) {
//            e.printStackTrace();
            LogManager.e(Constant.TAG, "createHttpsURLConnectionAndConnect: ", e);

        }
        return https;
    }

    public static HttpsURLConnection createHttpsURLConnection(String url) {
        HttpsURLConnection https = null;
        try {
            HttpConnection httpConnection = new HttpConnection();
            https = (HttpsURLConnection) new URL(url).openConnection();
            httpConnection.initHttpsConnection(https, 1);
        } catch (Throwable e) {

        }
        return https;
    }

    @SuppressLint("NewApi")
    protected void initHttpsConnection(HttpsURLConnection conn, int type)
            throws IOException {

        if (sslSocketFactory == null) {
            try {
                sslSocketFactory = Https.getInstance(type)
                        .getSslSocketFactory();
                // this.getSession().setSslSocketFactory(sslSocketFactory);
            } catch (Throwable ex) {
                if (ex instanceof IOException)
                    throw (IOException) ex;
                throw new IOException(ex);
            }
        }
        if (sslSocketFactory != null) {
            conn.setSSLSocketFactory(sslSocketFactory);
        }

        try {
            conn.setHostnameVerifier(Https.getInstance(type)
                    .getHostnameVerifier());
        } catch (Throwable e) {
            throw new IOException(e);
        }
        // 下面这段是信任所有证书的
        // try {
        // SSLContext sc = SSLContext.getInstance("TLS");
        // sc.init(null, new TrustManager[] { new MyTrustManager() },
        // new SecureRandom());
        // HttpsURLConnection
        // .setDefaultSSLSocketFactory(sc.getSocketFactory());
        // HttpsURLConnection
        // .setDefaultHostnameVerifier(new MyHostnameVerifier());
        // } catch (Throwable e) {
        // e.printStackTrace();
        // }
    }

}
